<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\RoleLogic;
use Web\Logic\CommonLogic;
class SensorController extends BaseController {
    
    public function sensor_list(){
        $sensor_name = $_REQUEST['sensor_name'];
        

        if(!empty($sensor_name)) $map['sensor_name'] = array('like', "%{$sensor_name}%");

        $count = M('device_sensor')->where($map)->count();
        $Page  = $this->getpage($count,50);
        $Page->parameter['sensor_name']   =   urlencode($sensor_name);
        
        $lists = M('device_sensor')
            ->alias('m')
            ->field("m.*")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('m.sorts,m.id asc')
            ->where($map)
            ->select();

        $this->assign("list",$lists);
        $show  = $this->bootstrap_page_style($Page->show());

        $this->assign('empty','<td colspan="4">没有数据</td>');
        $this->assign('page',$show);
         $this->assign('sensor_name',$sensor_name);
        $this->display();

    }
    
    public function saveSort(){
        $data = I('data');
        $Menu = M("device_sensor");
        foreach($data as $v){
            $Menu->where(array('id'=>$v['id']))->setField('sorts', $v['sort']);
        }
        echo '1';
    }



        /**
     * @desc 添加用户
     * @param rolename 用户名
     * @param notes 备注
     */
    public function sensor_add()
    {
        //实例化 logic
        //获取提交隐含域
        $subs = $_REQUEST['sub'];
        //接受用户名
        $data = $_REQUEST['data'];
        if($data)
        {
          // 
          $count = M('device_sensor')->count();
          $data['sorts'] =  $count +1;
          if($data['is_count'] == "on"){
                $data['is_count'] = 1;
          }else{
               $data['is_count'] = 0; 
          }
          $r = M('device_sensor')->add($data);
          $this->success('增加成功');
        }else{
      
        //调用模板
        $this->display();
        }
    }

    /**
     * @desc 用户删除
     * @param roleid 用户id
     */
    public function sensor_deletes()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('device_sensor')->where(array('id'=>$id))->delete();

        if($r)
        {
            $this->success('删除成功');
        }
    }

      public function sensor_copys()
    {
        //接受 roleid
        $id = $_REQUEST['id'];
        $r = M('device_sensor')->where(array('id'=>$id))->find();
        $r['sensor_name'] = $r['sensor_name']."copy";
        unset($r['id']);

        if($r)
        {
            $r = M('device_sensor')->add($r);
            $this->success('复制成功');
        }
    }

    /**
     * @desc 用户修改
     * @param roleid 用户id
     */
    public function sensor_edit()
    {
        //接受 用户id
        $id = $_REQUEST['id'];

        //接受表单提交
        $sub = $_REQUEST['sub'];
        //接受修改信息
        $data = $_REQUEST['data'];
        if($data && $sub)
        {
            //$data['sensor_dec'] =  $data['sensor_dec']?$data['sensor_dec']:"";
            if($data['is_count'] == "on"){
                $data['is_count'] = 1;
            }  else
            {
               $data['is_count'] = 0; 
            }
           $r = M('device_sensor')->where(array('id'=>$id))->save($data);
           
           $this->success('修改成功',"sensor_list");

        } else{


            //查询信息
            $datalist = M('device_sensor')->where(array('id'=>$id))->find();
            $this->assign('datalist',$datalist);
            $this->display();
        }
    }
    
    
    public function sensor_param()
    {
        //接受 用户id
        $sensor_code = $_REQUEST['sensor_code'];

        //查询信息
        $datalist = M('device_sensor_param')->where(array('sensor_code'=>$sensor_code))->select();
        $this->assign('datalist',$datalist);
        
        $sensor = M('device_sensor')->where(array('sensor_code'=>$sensor_code))->find();
        $this->assign('sensor_code',$sensor_code);
        $this->assign('sensor_name',$sensor['sensor_name']);
        $this->display();

    }
    
    public function sensor_param_update()
    {
        //接受 用户id
        $sensor_code = $_REQUEST['sensor_code'];
        //接受修改信息
        $data = $_REQUEST['data'];
        
        
        
        if($data['id']>0)
        {
           $r = M('device_sensor_param')->where(array('id'=>$data['id']))->save($data);
           echo $r;

        } else{
            $r = M('device_sensor_param')->add($data);
            
            echo M('device_sensor_param')->getLastInsID();
        }
    }
    
    
     public function sensor_param_delete($id)
    {
       $r = M('device_sensor_param')->where(array('id'=>$id))->delete();
       echo "1"; 
        
    }
    
    
    
    
    
}